#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
using ll = long long;
const int mod = 998244353;

vector<pair<ll, ll>> num, val;
ll cnt[N];

ll qpow(ll a, ll b) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

int main() {

  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 6; j++) {
      int k;
      scanf("%d", &k);
      num.push_back({k, i});
    }
  }

  sort(num.begin(), num.end());
  for (int i = 1; i <= n; i++)
    cnt[i] = 6;

  ll p = 1, ans = 0;

  while (!num.empty()) {

    ll max_val = num.back().first;
   
    val.push_back({p, max_val});

    while (!num.empty() && num.back().first == max_val) {
      int idx = num.back().second;
      (p *= (cnt[idx] - 1) % mod * qpow(cnt[idx], mod - 2) % mod) % mod;
      cout<<p<<endl;
      cnt[idx]--;
      num.pop_back();
    }
  }

  val.push_back({p, 0});

  // 注意，val中的值的顺序
  for (int i = 0; i < (int)val.size() - 1; i++) {
    cout << val[i].first << " " << val[i].second << endl;
    ll pro = (val[i].first + mod - val[i + 1].first) % mod;
    (ans += (pro * val[i].second) % mod) % mod;
  }

  printf("%d\n", ans);
  return 0;
}